//
//  RGCommentContentView.m
//  RedGrass
//
//  Created by wukong on 2019/1/22.
//  Copyright © 2019年 hongcaosp. All rights reserved.
//

#import "RGCommentContentView.h"
#import <UIButton+WebCache.h>

@interface RGCommentContentView()

@property (strong, nonatomic) UIButton *userBtn;
@property (strong, nonatomic) UILabel *userNameLabel;
@property (strong, nonatomic) UILabel *commentLabel;
@property (strong, nonatomic) UIButton *favoriteBtn;
@property (strong, nonatomic) UILabel *favoriteCount;

@property (strong, nonatomic) UIButton *actionBtn;


@end

@implementation RGCommentContentView


-(instancetype) initWithIsTapEnable:(BOOL)isEnable{
    if(self = [super init]){
        if(isEnable){
            [self insertSubview:self.actionBtn belowSubview:self.userBtn];
            [_actionBtn mas_makeConstraints:^(MASConstraintMaker *make) {
                make.edges.mas_equalTo(UIEdgeInsetsZero);
//                make.right.mas_equalTo(self).mas_offset(-70);
            }];
        }
    }
    return self;
}


-(instancetype)initWithFrame:(CGRect)frame{
    if(self = [super initWithFrame:frame]){
        [self initUIStyle];
        [self addMasonry];
    }
    return self;
}

-(void) initUIStyle{
    [self addSubview:self.userBtn];
    [self addSubview:self.userNameLabel];
    [self addSubview:self.commentLabel];
    [self addSubview:self.favoriteBtn];
    [self addSubview:self.favoriteCount];
}

-(void) addMasonry{
    
    [_userBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.mas_equalTo(CGSizeMake(40, 40));
        make.left.mas_equalTo(self).mas_offset(0);
        make.top.mas_equalTo(self).mas_offset(8);
    }];
    
    [_userNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.userBtn.mas_top).mas_offset(2);
        make.left.mas_equalTo(self.userBtn.mas_right).mas_offset(10);
        make.right.mas_lessThanOrEqualTo(self).mas_offset(-70);
    }];
    
    [_commentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.userNameLabel.mas_left).mas_offset(0);
        make.top.mas_equalTo(self.userNameLabel.mas_bottom).mas_offset(4);
        make.right.mas_equalTo(self).mas_offset(-70);
    }];
    
    [_favoriteBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self).mas_offset(18);
        make.right.mas_equalTo(self).mas_offset(-20);
        make.size.mas_equalTo(CGSizeMake(40, 40));
    }];
    
    [_favoriteCount mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.favoriteBtn.mas_centerX).mas_offset(0);
        make.top.mas_equalTo(self.favoriteBtn.mas_bottom).mas_offset(8);
    }];
}

#pragma mark - public

-(void) setCommenModel:(RGRecommentListModel *)commenModel{
    if(!commenModel){
        return;
    }
    _commenModel = commenModel;
    
    if([self commentUserIsSelfWithId:commenModel.commentsUser.ID]){//是自己
        RGUserInfoModel *user = [RGUserInfoModel getUserInfo];
        [_userBtn sd_setImageWithURL:[NSURL URLWithString:SafeStr(user.image)] forState:UIControlStateNormal placeholderImage:IMAGE_NAMED(@"头像")];
        NSString *nickName = user.nickname;
        if(![nickName length]){
            nickName = @"匿名用户";
        }
        _userNameLabel.text = nickName;
    }else{
        [_userBtn sd_setImageWithURL:[NSURL URLWithString:SafeStr(commenModel.commentsUser.image)] forState:UIControlStateNormal placeholderImage:IMAGE_NAMED(@"头像")];
        NSString *nickName = commenModel.commentsUser.nickname;
        if(![nickName length]){
            nickName = @"匿名用户";
        }
        _userNameLabel.text = nickName;
    }
    _commentLabel.text = commenModel.comment;
    if(commenModel.isLike){
        _favoriteBtn.selected = YES;
    }else{
        _favoriteBtn.selected = NO;
    }
    _favoriteCount.text = [self makeCountStrWithNumber:commenModel.likeCounts];
    
}

-(void) setChildCommenModel:(RGChildRecommentListModel *)childCommenModel{
    if(!childCommenModel){
        return;
    }
    _childCommenModel = childCommenModel;
    
    if([self commentUserIsSelfWithId:childCommenModel.commentsUser.ID]){//是自己
        RGUserInfoModel *user = [RGUserInfoModel getUserInfo];
        [_userBtn sd_setImageWithURL:[NSURL URLWithString:SafeStr(user.image)] forState:UIControlStateNormal placeholderImage:IMAGE_NAMED(@"头像")];
        NSString *nickName = user.nickname;
        if(![nickName length]){
            nickName = @"匿名用户";
        }
        _userNameLabel.text = nickName;
    }else{
        [_userBtn sd_setImageWithURL:[NSURL URLWithString:SafeStr(childCommenModel.commentsUser.image)] forState:UIControlStateNormal placeholderImage:IMAGE_NAMED(@"头像")];
        NSString *nickName = childCommenModel.commentsUser.nickname;
        if(![nickName length]){
            nickName = @"匿名用户";
        }
        _userNameLabel.text = nickName;
    }
   
    _commentLabel.text = childCommenModel.comment;
    if(childCommenModel.isLike){
        _favoriteBtn.selected = YES;
    }else{
        _favoriteBtn.selected = NO;
    }
    _favoriteCount.text = [self makeCountStrWithNumber:childCommenModel.likeCounts];
}

-(BOOL) commentUserIsSelfWithId:(NSInteger)ID{
    RGUserInfoModel *user = [RGUserInfoModel getUserInfo];
    return user.ID == ID ? YES : NO;
}


#pragma mark - private

-(NSString *)makeCountStrWithNumber:(NSInteger)num{
    if(num < 10000){
        return [NSString stringWithFormat:@"%ld",num];
    }
    float tempNum = num / 10000;
    return [NSString stringWithFormat:@"%0.1fW",ceil(tempNum)];
}

#pragma mark -reponse
-(void)userBtnClick:(UIButton *)button{
    if(self.actionBtnClickBlock){
        self.actionBtnClickBlock(kListCommentUserTag);
    }
}

-(void)favoriteBtnClick:(UIButton *)button{
    if(self.actionBtnClickBlock){
        self.actionBtnClickBlock(kListCommentFavoriteTag);
    }
}

-(void) actionBtnClick:(UIButton *)button{
    if(self.actionBtnClickBlock){
        self.actionBtnClickBlock(kListCommentRepeatTag);
    }
}

#pragma mark -getter

-(UIButton *)userBtn{
    if(!_userBtn){
        _userBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_userBtn addTarget:self action:@selector(userBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [_userBtn setImage:IMAGE_NAMED(@"头像") forState:UIControlStateNormal];
        ViewRadius(_userBtn, 20);
    }
    return _userBtn;
}

-(UILabel *)userNameLabel{
    if(!_userNameLabel){
        _userNameLabel = [[UILabel alloc]init];
        _userNameLabel.font = FONT(customFontName, 14);
        _userNameLabel.textColor = UICOLOR_WITH_RGBINT(0x777777);
        _userNameLabel.text = @"";
    }
    return _userNameLabel;
}

-(UILabel *)commentLabel{
    if(!_commentLabel){
        _commentLabel = [[UILabel alloc]init];
        _commentLabel.font = FONT(customFontName, 15);
        _commentLabel.numberOfLines = 0;
        _commentLabel.textColor = UICOLOR_WITH_RGBINT(0x333333);
        _commentLabel.text = @"";
    }
    return _commentLabel;
}

-(UIButton *)favoriteBtn{
    if(!_favoriteBtn){
        _favoriteBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_favoriteBtn addTarget:self action:@selector(favoriteBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [_favoriteBtn setImage:IMAGE_NAMED(@"未点赞") forState:UIControlStateNormal];
        [_favoriteBtn setImage:IMAGE_NAMED(@"点赞") forState:UIControlStateSelected];
        
    }
    return _favoriteBtn;
}

-(UILabel *)favoriteCount{
    if(!_favoriteCount){
        _favoriteCount = [[UILabel alloc]init];
        _favoriteCount.textColor = UICOLOR_WITH_RGBINT(0x666666);
        _favoriteCount.font = FONT(customFontName, 12);
        _favoriteCount.text = @"4444";
    }
    return _favoriteCount;
}

-(UIButton *)actionBtn{
    if(!_actionBtn){
        _actionBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_actionBtn addTarget:self action:@selector(actionBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _actionBtn;
}

@end
